<template>
  <div>

    <van-nav-bar
  title="文章详情"
  fixed
  left-arrow
  @click-left="$router.back()"
  class="header"
/>
    <!-- author -->
    <div class="author">
      <h1>{{article.title}}</h1>

      <div class="author_info">
          <img :src="article.aut_photo">
          <div class="author_name">
            <span>{{article.aut_name}}</span>
            <span>{{article.pubdate |  dateFormat}}</span>
          </div>
          <div class="attention">
              <button class="van-button van-button--info van-button--mini " @click="getCancelFollowData" v-if="article.is_followed">
              已关注
              </button>
               <button class="van-button van-button--info van-button--mini van-button--plain" @click="getFollowData" v-else>
             <van-icon name="plus" />关注
              </button>
          </div>
      </div>
     <div class="hr van-divider van-divider--hairline" ></div>
     <!-- 文章区 -->
     <div class="art-content" v-html="article.content"></div>
     <!-- 结束部分 -->

     <div class="End van-divider van-divider--hairline van-divider--content-center">End</div>
     <div>

       <div class="like_box">
          <button class="van-button van-button--danger van-button--small "  v-if="article.attitude === 1" @click="getNoLikeData">
            <van-icon name="good-job-o" />已点赞
          </button>
           <button class="van-button van-button--danger van-button--small van-button--plain" v-else @click="getLikeData">
          <van-icon name="good-job-o" />点赞
        </button>
       </div>

     </div>

    </div>
  <art-comment :artId="artId" :cmtSum="article"></art-comment>
  </div>

</template>

<script>
// 按需导入 API 接口
import { getDetailArt, getLike, getNoLike, getFollow, getCancelFollow } from '../../API/detailArtuclesAPI.js'
import artComment from '../../components/artComment.vue'

export default {
  props: ['artId'],
  data () {
    return {
      article: null

    }
  },
  methods: {
    // 获取文章信息
    async getDetail () {
      const { data: res } = await getDetailArt(this.artId)
      // console.log(res)
      if (res.message === 'OK') {
        // console.log(res)
        this.article = res.data

        if (this.attitude === 1) {
          this.article.attitude = true
        } else {
          this.article.attitude = false
        }
      }
    },

    // 获取文章点赞详情
    async getLikeData () {
      const { data: res } = await getLike(this.artId)
      if (res.message === 'OK') {
        this.$toast.success('点赞成功！')
        this.article.attitude = 1
      }
    },
    // 获取文章取消点赞详情
    async getNoLikeData () {
      const data = await getNoLike(this.artId)
      if (data.status === 204) {
        this.article.attitude = 0
        this.$toast.success('取消点赞成功！')
      }

      // console.log(this.artId)
    },
    // 获取关注详情
    async getFollowData () {
      // console.log(this.aut_id)
      const { data: res } = await getFollow(this.article.aut_id.toString())

      if (res.message === 'OK') {
        this.$toast.success('关注成功！')
        this.article.is_followed = true
      }
    },
    // 获取取消关注详情
    async getCancelFollowData () {
      const res = await getCancelFollow(this.article.aut_id.toString())
      if (res.status === 204) {
        this.$toast.success('取消关注成功！')
        this.article.is_followed = false
      }
    },

    changeRed (e) {
      console.log(e)
    }

  },
  created () {
    this.getDetail()
  },
  // 如果artid发生变动，则重新请求文章信息
  watch: {
    artId () {
      this.article = null
      this.getDetail()
    }

  },

  components: {
    artComment
  },
  beforeRouteLeave (to, from, next) {
    from.meta.top = window.scrollY
    setTimeout(() => {
      next()
    }, 0)
  }
}
</script>

<style lang="less" scoped>
*{
  margin: 0;
  padding: 0;
}
.author{
  width: 100%;
  margin-top: 1.22667rem;

  h1{
    font-size: .42667rem;
    font-weight: 700;
    padding: 0.3rem;

  }
  .author_info{
    display: flex;
    align-items: center;
    padding:0.2003rem 0.2405rem;

    img{
        width: 1.6rem;
        height: 1.6rem;
        border-radius: 50%;
        background-color: #f8f8f8;
        margin-right: 0.13333rem;
        border: none;
    }
    .author_name{
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;

        span{
          padding: 0.133rem 0;
          font-size: 0.3865rem;
        }
        span:nth-child(2){
          color: #666;
          font-size: 0.3084rem;
        }
    }
    .attention{
      flex: 1;
      display: flex;
      justify-content: end;
      padding-right: 0.4054rem;
      button{
        padding: 0.1351rem;
      }
    }
  }
  .van-divider{
    display: flex;
    align-items: center;
    margin: 0.42667rem 0;
    color: #969799;
    font-size: .37333rem;
    line-height: .64rem;
    border-color: #ebedf0;
    border-style: solid;
    border-width: 0;
  }
  .like_box{
    display: flex;
    justify-content: center;
  }

.van-button__content {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
}
.art-content{
    font-size: .32rem;
    line-height: .64rem;
    width: 90%;
    overflow-x: scroll;
    word-break: break-all;
    padding: 0 0.2703rem;

}

</style>
